import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

//首页
const index = r => require.ensure([], () => r(require('../views/home/index.vue')), 'index')
//订单
const order = r => require.ensure([], () => r(require('../views/order/order/order.vue')), 'order')
const serviceLog = r => require.ensure([], () => r(require('../views/order/serviceLog.vue')), 'serviceLog')
const orderMap = r => require.ensure([], () => r(require('../views/order/orderMap.vue')), 'orderMap')
//消息
const message = r => require.ensure([], () => r(require('../views/message/message.vue')), 'message')
const msgDetail = r => require.ensure([], () => r(require('../views/message/msgDetail.vue')), 'msgDetail')
//登录
const login = r => require.ensure([], () => r(require('../views/login/login.vue')), 'login')
const guide = r => require.ensure([], () => r(require('../views/login/guide.vue')), 'guide')
const forgot = r => require.ensure([], () => r(require('../views/forgot/forgot.vue')), 'forgot')
//注册
const register = r => require.ensure([], () => r(require('../views/register/register.vue')), 'register')
const addProfile = r => require.ensure([], () => r(require('../views/register/addProfile.vue')), 'addProfile')
const agreement = r => require.ensure([], () => r(require('../views/register/agreement.vue')), 'agreement')
const authInfo = r => require.ensure([], () => r(require('../views/register/authInfo.vue')), 'authInfo')
const skillsLabel = r => require.ensure([], () => r(require('../views/register/skillsLabel.vue')), 'skillsLabel')
const pending = r => require.ensure([], () => r(require('../views/register/pending.vue')), 'pending')
//我的
const profile = r => require.ensure([], () => r(require('../views/profile/profile.vue')), 'profile')
const info = r => require.ensure([], () => r(require('../views/profile/info/info.vue')), 'info')
const avatarCrop = r => require.ensure([], () => r(require('../views/profile/info/children/avatarCrop.vue')), 'avatarCrop')
const checkBack = r => require.ensure([], () => r(require('../views/profile/info/children/checkBack.vue')), 'checkBack')
//收益信息
const earningsInfo = r => require.ensure([], () => r(require('../views/earnings/earningsInfo.vue')), 'earningsInfo')
//提现管理
const cashManager = r => require.ensure([], () => r(require('../views/manager/cashManager.vue')), 'cashManager');
const cashing = r => require.ensure([], () => r(require('../views/manager/cashing.vue')), 'cashing');
const meansManager = r => require.ensure([], () => r(require('../views/manager/meansManager.vue')), 'meansManager')
const bankCard = r => require.ensure([], () => r(require('../views/manager/children/bankCard.vue')), 'bankCard')
const wechat = r => require.ensure([], () => r(require('../views/manager/children/wechat.vue')), 'wechat')
const alipay = r => require.ensure([], () => r(require('../views/manager/children/alipay.vue')), 'alipay')
const cashBack = r => require.ensure([], () => r(require('../views/manager/children/cashBack.vue')), 'cashBack')
//设置
const settings = r => require.ensure([], () => r(require('../views/settings/settings.vue')), 'settings')
const aboutUs = r => require.ensure([], () => r(require('../views/settings/aboutUs.vue')), 'aboutUs')
const modifyPwd = r => require.ensure([], () => r(require('../views/settings/modifyPwd.vue')), 'modifyPwd')
const feedback = r => require.ensure([], () => r(require('../views/settings/feedback.vue')), 'feedback')
//我的评价
const rating = r => require.ensure([], () => r(require('../views/rating/rating.vue')), 'rating')
//接单范围
const range = r => require.ensure([], () => r(require('../views/range/range.vue')), 'range')
const rangeMap = r => require.ensure([], () => r(require('../views/range/rangeMap.vue')), 'rangeMap')
Router.prototype.goBack = function () {
  this.isBack = true
  window.history.go(-1);
}

Vue.use(Router)

const router = new Router({
  mode: 'hash',
  routes: [
    {
      path: '/', redirect: '/index'
    },
    {
      path: '/index', name: 'index', component: index, meta: {requireAuth: true}
    },
    {
      path: '/order', name: 'order', component: order, meta: {requireAuth: true}
    },
    {
      path: '/order/serviceLog/:order_id/:arrIndex', name: 'serviceLog', component: serviceLog
    },
    {
      path: '/order/orderMap', name: 'orderMap', component: orderMap
    },
    {
      path: '/message', name: 'message', component: message, meta: {requireAuth: true}
    },
    {
      path: '/message/msgDetail', name: 'msgDetail', component: msgDetail
    },
    {
      path: '/login', name: 'login', component: login
    },
    {
      path: '/guide', name: 'guide', component: guide
    },
    {
      path: '/forgot', name: 'forgot', component: forgot
    },
    {
      path: '/register', name: 'register', component: register
    },
    {
      path: '/register/addProfile', name: 'addProfile', component: addProfile
    },
    {
      path: '/register/authInfo', name: 'authInfo', component: authInfo
    },
    {
      path: '/register/authInfo/skillsLabel', name: 'skillsLabel', component: skillsLabel
    },
    {
      path: '/register/pending', name: 'pending', component: pending
    },
    {
      path: '/register/agreement', name: 'agreement', component: agreement
    },
    {
      path: '/profile', name: 'profile', component: profile, meta: {requireAuth: true}
    },
    {
      path: '/profile/info', name: 'info', component: info, meta: {requireAuth: true},
      children: [
        {path: 'checkBack', name: 'checkBack', component: checkBack, meta: {requireAuth: true}},
        {path: 'avatarCrop', name: 'avatarCrop', component: avatarCrop, meta: {requireAuth: true}},
      ]
    },
    {
      path: '/range', name: 'range', component: range, meta: {requireAuth: true}
    },
    {
      path: '/range/rangeMap', name: 'rangeMap', component: rangeMap
    },
    {
      path: '/earningsInfo', name: 'earningsInfo', component: earningsInfo, meta: {requireAuth: true}
    },
    {
      path: '/manager/cashManager', name: 'cashManager', component: cashManager, meta: {requireAuth: true}
    },
    {
      path: '/manager/cashing', name: 'cashing', component: cashing, meta: {requireAuth: true},
      children: [
        {path: 'cashBack', name: 'cashBack', component: cashBack, meta: {requireAuth: true}}
      ]
    },
    {
      path: '/manager/meansManager', name: 'meansManager', component: meansManager,
      meta: {requireAuth: true},
      children: [
        {path: 'bankCard', name: 'bankCard', component: bankCard, meta: {requireAuth: true}},
        {path: 'wechat', name: 'wechat', component: wechat, meta: {requireAuth: true}},
        {path: 'alipay', name: 'alipay', component: alipay, meta: {requireAuth: true}}
      ]
    },
    {path: '/settings', name: 'settings', component: settings, meta: {requireAuth: true}},
    {path: '/aboutUs', name: 'aboutUs', component: aboutUs},
    {path: '/modifyPwd', name: 'modifyPwd', component: modifyPwd, meta: {requireAuth: true}},
    {path: '/feedback', name: 'feedback', component: feedback, meta: {requireAuth: true}},
    {
      path: '/rating', name: 'rating', component: rating, meta: {requireAuth: true}
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {
    let {token} = store.state.currentUser;
    if (token) {
      next();
    } else {
      next({
        path: '/login',
        // query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
});

export default router
